package com.airycode.distribution.distribution1.service;

import com.airycode.distribution.distribution1.domain.MemberEntity;
import com.airycode.distribution.distribution1.repository.MemberRepository;
import com.airycode.distribution.distribution1.utils.PageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zkr-fanxirui on 2017/8/14.
 */
@Service
public class MemberService {

    final Logger log = LoggerFactory.getLogger(MemberService.class);

    @Autowired
    private MemberRepository memberRepository;

    //分页查询
    public Map<String,Object> getProcFormByPage(Map<String,Object> paramMap){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        int pageNum = 0;
        int pageSize = 10;
        String formName = "";
        Object page_gotoPageNo = paramMap.get("page_gotoPageNo");
        if(null!=page_gotoPageNo){
            pageNum = Integer.parseInt(page_gotoPageNo.toString());
        }
        Object form_Name = paramMap.get("form_Name");
        if(null!=form_Name){
            formName = form_Name.toString();
        }
        Object page_Size = paramMap.get("page_Size");
        if(null!=page_Size){
            pageSize = Integer.parseInt(page_Size.toString());
        }
        String finalFormName = formName;
        log.info("..................finalFormName="+finalFormName+",page_Size="+pageSize+",pageNum="+pageNum);
        Specification<MemberEntity> specification = new Specification<MemberEntity>() {
            @Override
            public Predicate toPredicate(Root<MemberEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<MemberEntity> predicateList = new ArrayList<MemberEntity>();
                if(null!= finalFormName &&!"".equals(finalFormName)){
                    //predicateList.add(criteriaBuilder.like(root.get("memberName").as(String.class),"%"+finalFormName+"%"));//有问题分页
                }
                Predicate[] p = new Predicate[predicateList.size()];
                return criteriaBuilder.and(predicateList.toArray(p));
            }
        };
        Pageable pageable = new PageRequest(pageNum,pageSize, Sort.Direction.ASC,"id");
        Page<MemberEntity> procFormPage = memberRepository.findAll(specification,pageable);
        dataMap.put("realData",procFormPage);
        log.info("..................procFormPage.getNumber()="+procFormPage.getNumber()+",procFormPage.getTotalPages()="+procFormPage.getTotalPages());
        dataMap.put("pageItems", PageUtils.getItems(procFormPage.getNumber(),procFormPage.getTotalPages()));
        dataMap.put("form_Name",finalFormName);
        return dataMap;
    }

    //查询所有的会员
    public Map<String,Object> getMemberAll(){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        List<MemberEntity> members = memberRepository.selectAllMember();
        dataMap.put("members",members);
        return dataMap;
    }

    //添加会员
    public boolean addMember(MemberEntity memberEntity){
        MemberEntity entity= memberRepository.saveAndFlush(memberEntity);
        if (entity == null) {
            return false;
        }
        return true;
    }

    //删除会员
    public boolean deleteMember(String id){
        boolean result = true;
        try{
            memberRepository.delete(id);
        }catch(Exception e){
            result=false;
        }
        return result;
    }
    //根据id查找
    public MemberEntity searchMember(String id){
        return memberRepository.findOne(id);
    }




}
